package com.cmpan.networkdisk.service;

import com.cmpan.networkdisk.dao.DiskMapper;
import com.cmpan.networkdisk.model.Disk;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:DiskServiceImp
 * Package:com.cmpan.networkdisk.service
 * Description:
 *
 * @Date:2020/7/7 17:25
 * @com.chuangmei
 */
@Service
public class DiskServiceImp implements DiskService{

    @Resource
    private DiskMapper diskDao;


    //上传后写入数据库
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Map<String,Object> uploadFile(Disk disk) {

        //查询所有文件名,如果有同名,不可上传
        List<String> fileNames = diskDao.selectAllFileName();
        //构建map集合
        Map map = new HashMap<String,Object>();

        for (String name : fileNames) {
            if (disk.getFilename().equals(name)){
                System.out.println("已经存在此文件,返回0" + name);
                map.put("count",0);
                map.put("fileName",name);
                return map;
            }
        }

        //插入数据
        Integer count = diskDao.insert(disk);

        map.put("count",count);
        map.put("fileName",null);

        System.out.println("countservice = " + count);

        return map;
    }

    //查询所有的文件
    @Override
    public List<Disk> selectAll() {

        List<Disk> allfile = diskDao.selectAll();

        System.out.println("登录成功,查询所有数据: " + allfile);
        for (Disk disk : allfile) {
            System.out.println(disk);
        }

        return allfile;
    }

    //查询所有的文件分页
    @Override
    public List<Disk> selectAllPage(int start,int end,int total) {

        List<Disk> allfile = diskDao.selectPage((start-1)*end,end);

        System.out.println("登录成功,查询所有数据: " + allfile);
        for (Disk disk : allfile) {
            System.out.println(disk);
        }

        return allfile;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Integer deleteById(Long id) {

        //查询出路径删除
        String path = diskDao.selectFilePathByPrimaryKey(id);
        //删除path文件
        File file = new File(path);
        System.out.println(file.isFile());
        //如果是文件
        if (file.isFile()){
            //删除返回结果
            boolean deleteok = file.delete();
            System.out.println("iftrue: " + deleteok);
            if (!deleteok){
                //删除失败返回-1
                return -1;
            }
        }else{
            return -1;
        }

        //调用删除接口
        Integer count = diskDao.deleteByPrimaryKey(id);

        return count;
    }

    @Override
    public String selectPathById(Long id) {

        String path = diskDao.selectFilePathByPrimaryKey(id);

        //返回路径
        if (!"".equals(path) && path != null){
            return path;
        }

        return null;
    }

    @Override
    public List<Disk> deleteByFileName(String[] fileName) {

        //调用dao接口查询
        List<Disk> disks = diskDao.selectFileName(fileName);

        System.out.println("搜索传递的参数: " + fileName);
        System.out.println("搜索查询到的数据: " + disks);

        //测试
        /*for(Disk disk : disks){
            System.out.println(disk.toString());
        }*/

        return disks;
    }

    @Override
    public List<Disk> searchByTypeID(String typeid) {

        List<Disk> disks = diskDao.selectByTypeID(typeid);

        return disks;
    }

    @Override
    public int selectPage() {
        int count = diskDao.selectpage();
        return count;
    }

    @Override
    public Disk selectDiskById(Long valueOf) {

        Disk disk = diskDao.selectByPrimaryKey(valueOf);

        return disk;
    }

    @Override
    public int modifyByIdSelective(Disk disk) {

        int count = diskDao.updateByPrimaryKeySelective(disk);

        return count;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public StringBuffer batchDelete(String[] id) {
        List<Disk> disks = diskDao.selectDiskByIDs(id);

        StringBuffer sb = new StringBuffer();
        List<Disk> ids = new ArrayList<>();

        boolean fileDelete = true;
        int total = 0;
        for (Disk disk : disks){
            //拿到filepath
            String filePath = disk.getFilepath();
            //构建file
            File file = new File(filePath);
            boolean ok = file.delete();
            if (ok){
                /*//数据库删除
                diskDao.deleteByPrimaryKey(Long.valueOf(disk.getId()));*/
                ids.add(disk);
                total += 1;
            }else{
                sb.append(disk.getFilename() + " ");
                fileDelete = false;
            }
        }

        //数据库删除
        int count = diskDao.batchDelete(disks);
        if (fileDelete){
            if (count != total){
                sb.append("Failed");
            }else{
                sb.append("Success");
            }
        }

        return sb;
    }

}
